package com.epower.web.action.epoweruser;

import java.io.IOException;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Constants;
import com.epower.bo.epoweruserword.EpowerUserWordService;
import com.epower.dataobject.EpowerBookDO;
import com.epower.dataobject.EpowerUserDO;
import com.epower.dataobject.EpowerUserWordDO;
import com.epower.dataobject.EpowerWordDO;

/**
 * @author bolin.hbc
 * 
 */
@Scope("prototype")
@Component()
public class UserCenterAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EpowerUserWordDO query;

	private EpowerUserWordService epowerUserWordBO;

	private List<EpowerBookDO> bookList;

	private List<EpowerWordDO> wordList;

	/**
	 * 浏览课本学习的列表
	 * 
	 * @return
	 */
	public String doViewStudyList() {

		if (query == null) {
			query = new EpowerUserWordDO();
		}
		EpowerUserDO user = super.getUser();
		if (user == null) {
			return ERROR;
		}
		query.setUserid(user.getId());
		bookList = epowerUserWordBO.getBookListByUserId(query);
		return SUCCESS;
	}

	public String collections() {
		if (query == null) {
			query = new EpowerUserWordDO();
		}
		query.setStatus(Constants.STATUS_OK);
		EpowerUserDO user = super.getUser();
		if (user == null) {
			return ERROR;
		}
		query.setUserid(user.getId());
		wordList = epowerUserWordBO.getUserCollections(query);
		return SUCCESS;
	}

	private Integer wordid;

	/**
	 * 取消收藏
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public void releaseCollections() throws JSONException, IOException {
		boolean isOk = epowerUserWordBO.releaseCollections(getUser().getId(), wordid);
		JSONObject obj = new JSONObject();
		obj.put("isSuccess", isOk);
		this.write(obj.toString());
	}

	public void changeLevel() throws JSONException, IOException {
		query.setUserid(this.getUser().getId());
		int level = query.getLevel();
		query.setLevel(null);
		EpowerUserWordDO userWord = epowerUserWordBO.getEpowerUserWordByOption(query);
		userWord.setLevel(level);
		boolean isOk = epowerUserWordBO.updateEpowerUserWordByOption(userWord);
		JSONObject obj = new JSONObject();
		obj.put("isSuccess", isOk);
		this.write(obj.toString());
	}

	public String doViewUserWordList() {
		if (query == null) {
			query = new EpowerUserWordDO();
		}
		// 生词
		query.setUserid(super.getUser().getId());
		wordList = epowerUserWordBO.getUserWordsByUserId(query);
		return SUCCESS;
	}

	public EpowerUserWordDO getQuery() {
		return query;
	}

	public void setQuery(EpowerUserWordDO query) {
		this.query = query;
	}

	public void setEpowerUserWordBO(EpowerUserWordService epowerUserWordBO) {
		this.epowerUserWordBO = epowerUserWordBO;
	}

	public EpowerUserWordService getEpowerUserWordBO() {
		return epowerUserWordBO;
	}

	public List<EpowerBookDO> getBookList() {
		return bookList;
	}

	public List<EpowerWordDO> getWordList() {
		return wordList;
	}

	public void setWordid(Integer wordid) {
		this.wordid = wordid;
	}

}
